/**
 * 卷帘对比(cesium原生实现)
 *
 * @param   viewer          cesium实例
 * @param   showLine        是否显示分割线
 */

let split = null

export function initBQGIS_split({
    viewer = window.viewer,
    showLine = true,
    domStyle = {}
}) {
    if(!split){
        split = new BQGIS_split({
            viewer,
            showLine,
            domStyle
        })
    }
    return split
}

export function destroyBQGIS_split(){
    split.viewerDom.removeChild(split.sliderDom)
    split.handler.destroy()
    split = undefined
}

export function clear(){
    
}

/**
 * 切换(左/右屏)
 *
 * @param   obj             Cesium3DTileset/ImageryLayer 实体
 * @param   splitDirection  left/right/none
 */
export function setSplitDirection(obj, splitDirection) {
    const direction = splitDirection.toUpperCase()
    if (obj instanceof Cesium.Cesium3DTileset || obj instanceof Cesium.ImageryLayer) {
        obj.splitDirection = Cesium.SplitDirection[direction]
    }
}

/**
 * 获取方向
 *
 * @param   obj             Cesium3DTileset/ImageryLayer 实体
 * 
 * @reture  left/right/none
 */
export function getSplitDirection(obj) {
    if (obj instanceof Cesium.Cesium3DTileset || obj instanceof Cesium.ImageryLayer) {
        const directionMap = {
            [Cesium.SplitDirection.LEFT]: 'left',
            [Cesium.SplitDirection.RIGHT]: 'right',
            [Cesium.SplitDirection.NONE]: 'none'
        }
        return directionMap[obj.splitDirection]
    }
}


/**
 * 批量切换(左/右屏)
 *
 * @param   arr             实体数组
 * @param   splitDirection  left/right/none
 */
export function mSetSplitDirection(arr, splitDirection) {
    const direction = splitDirection.toUpperCase()
    arr.forEach(item => {
        setSplitDirection(item, direction)
    })
}

export function BQGIS_split({
    viewer = window.viewer,
    showLine,
    domStyle = {}
}) {
    this.viewer = viewer
    this.viewerDom = viewer.container
    this.sliderDom = createSplitDom(this.viewerDom, domStyle)
    this.handler = new Cesium.ScreenSpaceEventHandler(this.sliderDom)
    this.onMoveEvent()
    this.showLine = showLine
    showLine ? this.show() : this.hide()
}


BQGIS_split.prototype.show = function () {
    this.showLine = true
    this.sliderDom.style.display = 'block'
    viewer.scene.splitPosition = this.sliderDom.offsetLeft / this.viewerDom.offsetWidth;
}

BQGIS_split.prototype.hide = function () {
    this.showLine = false
    this.sliderDom.style.display = 'none'
}

BQGIS_split.prototype.add = function (layer, splitDirection) {
    let direction = splitDirection.toUpperCase()
}

BQGIS_split.prototype.onMoveEvent = function () {
    let moveActive = false
    this.handler.setInputAction(function () {
        moveActive = true;
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);

    this.handler.setInputAction((movement) => {
        if (!moveActive) {
            return;
        }
        const relativeOffset = movement.endPosition.x;
        const splitPosition =
            (this.sliderDom.offsetLeft + relativeOffset) /
            this.viewerDom.offsetWidth;
        this.sliderDom.style.left = `${100.0 * splitPosition}%`;
        viewer.scene.splitPosition = splitPosition;
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    this.handler.setInputAction(function () {
        moveActive = false;
    }, Cesium.ScreenSpaceEventType.LEFT_UP);
}

function createSplitDom(viewerDom, domStyle) {
    viewerDom.style.position = 'relative'
    const dom = document.createElement("div")
    dom.style.display = 'none'
    dom.style.position = 'absolute'
    dom.style.left = '50%'
    dom.style.top = '0px'
    dom.style.background = "linear-gradient(90deg, rgba(0, 0, 0, 0) 0%, rgba(255, 255, 255, 1) 50%, rgba(0, 0, 0, 0) 100%)"
    dom.style.width = '5px'
    dom.style.height = '100%'
    dom.style.zIndex = 9999
    Object.assign(dom.style, domStyle)
    viewerDom.appendChild(dom)
    return dom
}